import sys
import socket
import thread
import threading

def handler(clientsock, addr):
    while 1:
        data = clientsock.recv(bufsiz)
        if data == "stop":
            break
            clientsock.close()
        if not data:
            break        
        msg = "echoed:..." + data
        print data
        clientsock.send(msg)
    clientsock.close()

if __name__=="__main__":
    host = "127.0.0.1"
    port = 1234
    bufsiz = 1024
    addr = ((host,port))
    serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversock.bind(addr)
    serversock.listen(2)

    while 1:
        print "waiting for connection.."
        clientsock, addr = serversock.accept()
        print "..connected from:", addr
        thread.start_new_thread(handler, (clientsock, addr))
conn.close()


